package com.doublefish.doublefishrobot.ui.home.adapter;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.doublefish.doublefishrobot.R;
import com.doublefish.doublefishrobot.ui.home.bean.ModeBean;
import com.doublefish.doublefishrobot.ui.home.bean.ModeSettingBean;

import java.util.List;

/**
 * 作者:dugaofeng on 2024/10/18 16:46
 */
public class ModeSectionAdapter extends BaseMultiItemQuickAdapter<ModeSectionAdapter.ModeMultipleEntity, BaseViewHolder> {

    private boolean isSearching = false;  //是否是展示搜索结果

    public ModeSectionAdapter(List<ModeMultipleEntity> data) {
        super(data);

        addItemType(ModeMultipleEntity.HEADER, R.layout.item_product_type_header);
        addItemType(ModeMultipleEntity.CELL, R.layout.item_product_type_cell);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, ModeSectionAdapter.ModeMultipleEntity multiItemEntity) {
        // 根据返回的 type 分别设置数据
        switch (helper.getItemViewType()) {
            case ModeMultipleEntity.HEADER: {
                helper.setText(R.id.tv_text, multiItemEntity.getModeBean().getName());
            }
            break;
            case ModeMultipleEntity.CELL: {
                String name = null;
                if (isSearching) {
                    name = multiItemEntity.getModeSettingBean().getName() + "（" + multiItemEntity.getModeSettingBean().getDifficultyName() + "）";
                } else {
                    name = multiItemEntity.getModeSettingBean().getName();
                }
                helper.setText(R.id.tv_text, name);
            }
            break;
            default:
                break;
        }

        helper.setVisible(R.id.tv_line , multiItemEntity.isShowLine());
    }

    public void setSearching(boolean searching) {
        isSearching = searching;
    }

    public static class ModeMultipleEntity implements MultiItemEntity {

        public static final int HEADER = 0;
        public static final int CELL = 1;

        private boolean isHeader;
        private ModeBean modeBean;
        private ModeSettingBean modeSettingBean;

        //这个字段用于展示展示分割线，header和cell的逻辑有点区别
        // 对于header的分割线，默认展示  有子类型展示，没有子类不展示（但最后一条要展示）
        // 对于cell的分割线  默认展示，(最后一条不展示，但如果是全部数据的最后一条要展示)
        private boolean isShowLine;

        public void setShowLine(boolean showLine) {
            isShowLine = showLine;
        }

        public boolean isShowLine() {
            return isShowLine;
        }

        public ModeBean getModeBean() {
            return modeBean;
        }

        public void setModeBean(ModeBean modeBean) {
            this.modeBean = modeBean;
        }

        public ModeSettingBean getModeSettingBean() {
            return modeSettingBean;
        }

        public void setModeSettingBean(ModeSettingBean modeSettingBean) {
            this.modeSettingBean = modeSettingBean;
        }

        public boolean isHeader() {
            return isHeader;
        }

        public ModeMultipleEntity(boolean isHeader) {
            this.isHeader = isHeader;
            this.isShowLine = true; // 默认展示
        }

        @Override
        public int getItemType() {
            return isHeader ? HEADER : CELL;
        }
    }
}